﻿function btnLogin_Click()
{
    if(CheckInputValid() == false)
        return ;
    else
    {
        var body = GetBody();
            
        request = new XMLHttpRequest();
        request.open("POST", document.forms.frmRegister.action);  // HTTP POST, action Form1
        request.onreadystatechange = signUpCallBack;              // callback to function callBack
        request.setRequestHeader("SignUpEmployer", "true");  // registry RequestHeader LoadAllPlaces
        request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
        request.send(body);
    }
}

function signUpCallBack()
{
    if (request.readyState == 4 && request.status == 200) 
    {
        // xml is sended from server
        var responseXML = request.responseXML;
        var root = responseXML.documentElement;
        
        for (var i = 0; i < root.childNodes.length; i++) 
        {
            // get data detail Place from XML
            var isSuccess = root.childNodes[i].attributes.getNamedItem("IsSuccess").nodeValue;
            var username = root.childNodes[i].attributes.getNamedItem("Username").nodeValue;
            window.location = "TrangNhaTuyenDung.aspx?user=" + username;
        }
    }
}

// transfer detail Place to Server
function serializeSignupEmployer(username, password, confirm_password, fullname, position, email, address, city, phone, mobile, companyName, 
    companyLink, companyDescription)
{
    var requestBody = "";
    requestBody += "username";
    requestBody += "=";
    requestBody += encodeURIComponent(username);
    requestBody += "&";
    requestBody += "password";
    requestBody += "=";
    requestBody += encodeURIComponent(password);
    requestBody += "&";
    requestBody += "confirm_password";
    requestBody += "=";
    requestBody += encodeURIComponent(confirm_password);
    requestBody += "&";
    requestBody += "fullname";
    requestBody += "=";
    requestBody += encodeURIComponent(fullname);
    requestBody += "&";
    requestBody += "position";
    requestBody += "=";
    requestBody += encodeURIComponent(position);
    requestBody += "&";
    requestBody += "email";
    requestBody += "=";
    requestBody += encodeURIComponent(email);
    requestBody += "&";
    requestBody += "address";
    requestBody += "=";
    requestBody += encodeURIComponent(address);
    requestBody += "&";
    requestBody += "city";
    requestBody += "=";
    requestBody += encodeURIComponent(city);
    requestBody += "&";
    requestBody += "phone";
    requestBody += "=";
    requestBody += encodeURIComponent(phone);
    requestBody += "&";
    requestBody += "mobile";
    requestBody += "=";
    requestBody += encodeURIComponent(mobile);
    requestBody += "&";
    requestBody += "companyName";
    requestBody += "=";
    requestBody += encodeURIComponent(companyName);
    requestBody += "&";
    requestBody += "companyLink";
    requestBody += "=";
    requestBody += encodeURIComponent(companyLink);
    requestBody += "&";
    requestBody += "companyDescription";
    requestBody += "=";
    requestBody += encodeURIComponent(companyDescription);
    return requestBody;
}

function GetBody()
{
    var username = document.getElementById("username").value;
    var password = document.getElementById("password").value;
    var confirm_password = document.getElementById("confirm_password").value;
    var fullname = document.getElementById("fullname").value;
    var position = document.getElementById("position").value;
    var email = document.getElementById("email").value;
    var address = document.getElementById("address").value;
    var city = document.getElementById("city").value;
    var phone = document.getElementById("phone").value;
    var mobile = document.getElementById("mobile").value;
    var companyName = document.getElementById("companyName").value;
    var companyLink = document.getElementById("companyLink").value;
    var companyDescription = document.getElementById("companyDescription").value;
    
    return serializeSignupEmployer(username, password, confirm_password, fullname, position, email, address, city, phone, mobile, companyName, 
    companyLink, companyDescription);
}

function CheckInputValid()
{
    var username = document.getElementById("username").value;
    var password = document.getElementById("password").value;
    var confirm_password = document.getElementById("confirm_password").value;
    var fullname = document.getElementById("fullname").value;
    var position = document.getElementById("position").value;
    var email = document.getElementById("email").value;
    var address = document.getElementById("address").value;
    var city = document.getElementById("city").value;
    var phone = document.getElementById("phone").value;
    var mobile = document.getElementById("mobile").value;
    var companyName = document.getElementById("companyName").value;
    var companyLink = document.getElementById("companyLink").value;
    var companyDescription = document.getElementById("companyDescription").value;
    
    if(username == "")
    {   
        alert("Vui lòng nhập Tên Đăng Nhập!");
        return false;
    }
    if(password == "")
    {   
        alert("Vui lòng nhập M ật khẩu!");
        return false;
    }
    if(confirm_password == "")
    {   
        alert("Vui lòng nhập Xác nhận mật khẩu!");
        return false;
    }
    if(fullname == "")
    {   
        alert("Vui lòng nhập Họ Tên!");
        return false;
    }
    if(position == "")
    {   
        alert("Vui lòng nhập Chức vụ!");
        return false;
    }
    if(email == "")
    {   
        alert("Vui lòng nhập Email!");
        return false;
    }
    if(address == "")
    {   
        alert("Vui lòng nhập Địa chỉ!");
        return false;
    }
    if(city == "")
    {   
        alert("Vui lòng nhập Tên thành phố!");
        return false;
    }
    if(phone == "")
    {   
        alert("Vui lòng nhập SDT bàn!");
        return false;
    }
    if(mobile == "")
    {   
        alert("Vui lòng nhập DTDD!");
        return false;
    }
    if(companyName == "")
    {   
        alert("Vui lòng nhập Tên Công ty!");
        return false;
    }
    if(companyLink == "")
    {   
        alert("Vui lòng nhập Website!");
        return false;
    }
    if(companyDescription == "")
    {   
        alert("Vui lòng nhập Mô tả công ty!");
        return false;
    }
}